﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Keepo.Models;
using Keepo.Models.Services;
using Infrastructure.Data;
using System.Data.Objects;

namespace Keepo.Helpers
{
    public class PrintHelper
    {
        // Fits R1 inside R2
        // Get a new Rectangle with new sizes and offset
        static public Rect fitRect(float w1, float h1, float w2, float h2)
        {
            float s1 = w1 / w2;
            float s2 = h1 / h2;

            float scale = Math.Min(s1, s2);

            Rect result = new Rect();
            result.Width = w2 * scale;
            result.Height = h2 * scale;

            result.OffsetX = (w1 - result.Width) / 2;
            result.OffsetY = (h1 - result.Height) / 2;

            return result;
        }


        static public DesignPage designableProductPageByIdAndIdx(int designableProductId, int productPageIdx)
        {
            using (GenericRepository repository = new GenericRepository())
            {
                DesignableProduct product = repository.First<DesignableProduct>(p => p.DesignableProductId == designableProductId);

                DesignPage designPage = (from page in product.DesignPages
                                         where page.ProductPage.PageIdx == productPageIdx
                                         select page).FirstOrDefault();
                return designPage;
            }
        }

        static public TemplatePage templatePageByIdAndIdx(int templateId, int templatePageIdx)
        {
            using (GenericRepository repository = new GenericRepository())
            {
                DesignTemplate template = repository.First<DesignTemplate>(p => p.DesignTemplateId == templateId);

                TemplatePage templatePage = (from page in template.TemplatePages
                                             where page.PageIdx == templatePageIdx
                                             select page).FirstOrDefault();

                return templatePage;
            }
        }


        static public PrintMap calcPrintMap(DesignPage designPage, TemplatePage templatePage, PrintSize printSize)
        {
            PrintMap map = new PrintMap();

            Rect fittedRect = fitRect(printSize.WebsiteWidthInch, printSize.WebsiteHeightInch,
                templatePage.TemplateWidthPix, templatePage.TemplateHeightPix);

            if (designPage != null)
            {
                map.DPI = (int)((float)templatePage.TemplateWidthPix / fittedRect.Width);
                map.URL = templatePage.ImageURL;

                // Calculate the Offset and Scale
                float offsetXInch = fittedRect.OffsetX + (printSize.WebsiteOffsetXInch ?? 0);
                float offsetYInch = fittedRect.OffsetY + (printSize.WebsiteOffsetYInch ?? 0);

                float innerOffsetXInch = (float)templatePage.ImageRoiOffsetXPix / map.DPI;
                float innerOffsetYInch = (float)templatePage.ImageRoiOffsetYPix / map.DPI;

                offsetXInch += innerOffsetXInch;
                offsetYInch += innerOffsetYInch;

                map.OffsetXInMM = (int)(offsetXInch * 25.4f);
                map.OffsetYInMM = (int)(offsetYInch * 25.4f);
            }

            return map;
        }


        static public Rect EffectivePrintSize(int? templatePageId, int designableProductId, int designPageId)
        {
            using (GenericRepository repository = new GenericRepository())
            {
                Rect effectiveSize = new Rect();

                TemplatePage templatePage = repository.First<TemplatePage>(p => p.TemplatePageId == templatePageId);
                DesignPage designPage = repository.First<DesignPage>(p => p.DesignPageId == designPageId);

                PrinterPage printerPage = designPage.DesignableProduct.PrinterPages.FirstOrDefault(p => p.PageIdx == designPage.ProductPage.PageIdx);

                if (designPage != null && templatePage != null)
                {
                    PrintSize printSize = printerPage.PrintSize;

                    Rect fittedRect = PrintHelper.fitRect(printSize.WebsiteWidthInch, printSize.WebsiteHeightInch,
                        templatePage.TemplateWidthPix, templatePage.TemplateHeightPix);

                    // TBD: Verify this
                    float dpi = ((float)templatePage.TemplateWidthPix / fittedRect.Width);
                    effectiveSize.Width = templatePage.ImageRoiWidthPix / dpi;
                    effectiveSize.Height = templatePage.ImageRoiHeightPix / dpi;
                }

                return effectiveSize;
            }
        }

        // TBD: Location of this parameter
        public static float printSizeThresholdInch = 5.5f;
        public static float printAreaThresholdSqInch = printSizeThresholdInch * printSizeThresholdInch;

        static public bool isLargePrint(int? templatePageId, int designableProductId, int productPageId)
        {
            Rect effectiveSize = EffectivePrintSize(templatePageId, designableProductId, productPageId);
            float effectiveArea = effectiveSize.Width * effectiveSize.Height;

            return effectiveArea > printAreaThresholdSqInch;
        }


        static public string GenerateFileName(PrintSize printSize, PrintConfig printConfig, PrintMap map)
        {
            // setup_table_filename_copies_xshiftmm_yshiftmm.png
            String setupName = printConfig.Title;
            String tableName = printSize.PrintTable.Title;
            String fileName = "myTshirt";
            String copies = "1";
            String xshiftmm = map.OffsetXInMM.ToString();
            String yshiftmm = map.OffsetYInMM.ToString();

            String fullName = setupName + "_";
            fullName += tableName + "_";
            fullName += fileName + "_";
            fullName += copies + "_";
            fullName += xshiftmm + "_";
            fullName += yshiftmm + ".png";

            return fullName;
        }

    }


}